package org.tnc.hr.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static ArrayList<String> getAllDay(Date date) {

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");

        // 设置日期
        cal.setTime(date);

        // 当前月的第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        // String beginTime1 = datef.format(beginTime);

        // 获取当月天数
        int j = cal.getActualMaximum(Calendar.DATE);

        // 将当月第一天set
        cal.setTime(beginTime);

        ArrayList<String> list = new ArrayList<String>();
        for (int i = 1; i <= j; i++) {
            cal.set(Calendar.DATE, i);
            list.add(sdf.format(cal.getTime()));
//             System.out.println(sdf.format(cal.getTime()));
        }
        return list;
    }

    public static Date getStartDateByMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 设置日期
        cal.setTime(date);

        // 当前月的第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();

        return beginTime;
    }

    public static Date getEndDateByMonth(Date date) {

        Calendar cal = Calendar.getInstance();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 设置日期
        cal.setTime(date);

        // 当前月的最后一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        Date endTime = cal.getTime();

        return endTime;

    }

    public static Date convertDate(String adateStrteStr) {
        java.util.Date date = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = simpleDateFormat.parse(adateStrteStr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }

    public static Date convertDateHMS(String adateStrteStr) {
        java.util.Date date = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = simpleDateFormat.parse(adateStrteStr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }

    /**
     * 计算两个时间之间相隔天数
     * 
     * @param startday 开始时间
     * @param endday 结束时间
     * @return 间隔天数
     */
    public static int getIntervalDays(Date startday, Date endday) {
        // 确保startday在endday之前
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        // 分别得到两个时间的毫秒数
        long sl = startday.getTime();
        long el = endday.getTime();

        long ei = el - sl;
        // 根据毫秒数计算间隔天数
        return (int) (ei / (1000 * 60 * 60 * 24));
    }

    /**
     * 计算两个时间之间相隔分钟数
     * 
     * @param startday 开始时间
     * @param endday 结束时间
     * @return 间隔天数
     */
    public static float getIntervalHours(Date startday, Date endday) {
        // 确保startday在endday之前
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        // 分别得到两个时间的毫秒数
        long sl = startday.getTime();
        long el = endday.getTime();

        System.out.println(startday);
        System.out.println(endday);
        System.out.println(sl);
        System.out.println(el);

        long ei = el - sl;
        // 根据毫秒数计算间隔分钟数
        // float eif = Float.valueOf(String.valueOf(ei));
        // return (float) (eif / (1000 * 60 * 60));
        return (int) (ei / (1000 * 60 * 60));
    }

    public static ArrayList<String> listWeekend(int year, int month) { // 输入年，月，打印出该年中该月所有不是双休日的天数,不考虑输入年，月错误的情况下
        int day = getDay(year, month);
        ArrayList<String> weekendList = new ArrayList<String>();
        for (int i = 1; i <= day; i++) {
            if (getIsWeekend(year, month, i)) {
//                System.out.print(i + " ");
//                System.out.println(year + "-" + month + "-" + i);
                weekendList.add(year + "-" + month + "-" + i);
            }
        }
        return weekendList;
    }

    public static boolean getIsWeekend(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1, day); // 设置日期
        int week = c.get(Calendar.DAY_OF_WEEK); // 获取当前日期星期，英国那边的算法(周日算一周第一天)
        if (week == 7 || week == 1) { // 如果是周六或周日就返回true
            return true;
        }
        return false;
    }

    public static int getDay(int year, int month) { // 输入年，月，判断该年该月有多少天,不考虑输入年，月错误的情况下
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        } else if (month != 2) {
            return 30;
        } else {
            if (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0))) { // 判断当年是否是闰年
                return 29;
            } else {
                return 28;
            }
        }
    }

//    public static void main(String[] args) {
//        getAllDay(new Date());
//    }

}
